package com.zwg.git_customview.viewpager2

import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.ComponentActivity
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.zwg.git_customview.R
import com.zwg.git_customview.viewpagerlooperlayout.utils.SizeUtils
import com.zwg.git_customview.viewpager2.view.IndicatorView

/**
 *
 */
class RecycleVIewIndicatorViewActivity : ComponentActivity() {

    private var scrollX = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycleviewindicatorview)

        val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
        val indicatorView: IndicatorView = findViewById(R.id.indicatorView)

        val adapter = MyAdapter(this)
        recyclerView.adapter = adapter
        recyclerView.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)

        recyclerView.addItemDecoration(ItemDecoration())
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                scrollX += dx;
                val layoutManager = recyclerView.layoutManager as LinearLayoutManager
                val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
                indicatorView.setIndicatorCount(adapter.itemCount)
                indicatorView.setSelectedIndex(firstVisibleItemPosition)
                Log.d(
                    "TAG",
                    "onScrolled: recyclerView.scrollX = ${scrollX}, firstVisibleItemPosition=$firstVisibleItemPosition"
                )
                if (adapter.itemCount > 3) {
                    if (firstVisibleItemPosition >= 0 && firstVisibleItemPosition < adapter.itemCount - 3) {
                        Log.d("TAG", "adapter.itemCount > 3")
                        indicatorView.setSelectedIndex(firstVisibleItemPosition)
                    } else {
                        Log.d("TAG", "adapter.itemCount --------------- 3")
                        if (scrollX == 0) {
                            indicatorView.setSelectedIndex(0)
                        } else if (scrollX < (adapter.itemCount - 3) * 215 && scrollX > (adapter.itemCount - 3) * 200) {
                            indicatorView.setSelectedIndex(adapter.itemCount - 3)
                        } else if (scrollX < (adapter.itemCount - 2) * 215  && scrollX > (adapter.itemCount - 2) * 200) {
                            indicatorView.setSelectedIndex(adapter.itemCount - 2)
                        } else if (scrollX < (adapter.itemCount - 1) * 215  && scrollX > (adapter.itemCount - 1) * 200) {
                            indicatorView.setSelectedIndex(adapter.itemCount - 1)
                        }
                    }
                } else if (adapter.itemCount == 3) {
                    if (scrollX == 0) {
                        indicatorView.setSelectedIndex(0)
                    } else if (scrollX in 1..100) {
                        indicatorView.setSelectedIndex(adapter.itemCount - 2)
                    } else {
                        indicatorView.setSelectedIndex(adapter.itemCount - 1)
                    }
                }
            }
        })
    }

    private class MyAdapter(var context: Context) : RecyclerView.Adapter<MyViewHolder>() {


        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_skin_effect_pic, parent, false)
            return MyViewHolder(view)
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            // 在这里设置每个 item 的数据和点击事件等
            val url: String =
                "https://universal-test-1301203089.file.myqcloud.com/content/94361c34c9b284affdd13878d74a630e/58feb4762daf187370e99b5e27af988d.png"
            if (!TextUtils.isEmpty(url)) {
                Glide.with(context)
                    .load(url)
                    .override(SizeUtils.dip2px(context, 142f), SizeUtils.dip2px(context, 300f))
                    .centerCrop()
                    .transform(RoundedCorners(SizeUtils.dip2px(context, 12f)))
                    .placeholder(R.drawable.ic_launcher_background)
                    .into(holder.image)
            }
        }

        override fun getItemCount(): Int {
            return 4 // 设置总共有4个item
        }
    }

    private class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val image: AppCompatImageView = itemView.findViewById(R.id.item_skin_effect_pic_img)
    }

    private class ItemDecoration : RecyclerView.ItemDecoration() {
        // 可以在这里设置 item 之间的间距等样式
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            super.getItemOffsets(outRect, view, parent, state)
            val position = parent.getChildAdapterPosition(view)
            if (position == 0) {
                outRect.left = SizeUtils.dip2px(view.context, 20f) // 设置左侧间距
            } else {
                outRect.left = SizeUtils.dip2px(view.context, 10f) // 设置左侧间距
            }
        }

    }
}
